//
// Created by liule on 2021/8/13.
//

#include "gamewidget.h"

//
// Created by liule on 2021/8/12.
//

#include <QPushButton>
#include <QPainter>


GameWidget::GameWidget(QWidget *parent) : QWidget(parent) {
    //
    setFocusPolicy(Qt::StrongFocus);

    isStart = false;
    curKey = Qt::Key_D;

    this->initGame();

    timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(refreshGame()));
    timer->start(200);

}


void GameWidget::keyPressEvent(QKeyEvent *ev) {

    if(snake->pHead == NULL) {
        return;
    }

    curKey = ev->key();

}

void GameWidget::initGame() {

    wall = new Wall();
    food = new Food(wall);
    food->setFond();
    snake = new Snake(wall);

}

void GameWidget::refreshGame() {

    if(!isStart) {
        return;
    }

    int x = snake->pHead->x;
    int y = snake->pHead->y;
    //cout << "x:" << x << ",y=" << y << endl;

    int k = curKey;
    switch (k) {
        case Qt::Key_A:
            x--;
            break;
        case Qt::Key_D:
            x++;
            break;
        case Qt::Key_W:
            y--;
            break;
        case Qt::Key_S:
            y++;
            break;
        default:
            return;
    }

    char c = wall->getGameArr(x, y);
    //cout << "x:" << x << ",y=" << y << ",c:" << c << endl;
    if(c == '*' || c == '=') {
        cout << "game over..." << endl;
        stopGame();
        return;
    }

    //
    if(c == '#') {
        food->setFond();
        snake->addPoint(x, y);
    } else {
        snake->addPoint(x, y);
        snake->deleteEndPoint();
    }
    update();
}

void GameWidget::paintEvent(QPaintEvent *) {

    QPainter p(this);
    QPen oldPen = p.pen();

    /***************************** 墙 ****************************************/
    for(int i=0; i<wall->getRow(); i++) {
        for(int j=0; j<wall->getCol(); j++) {
            QString game((1, wall->getGameArr(j, i)));
            if(wall->getGameArr(j, i) == '@') {
                p.setPen(QPen(Qt::red, 3));
                p.setBrush(Qt::red);
                p.drawRect(QRect(j * default_multiple, i * default_multiple, default_multiple, default_multiple));
            } else if(wall->getGameArr(j, i) == '=') {
                p.setPen(QPen(Qt::green, 3));
                p.setBrush(Qt::green);
                p.drawRect(QRect(j * default_multiple, i * default_multiple, default_multiple, default_multiple));
            }  else if(wall->getGameArr(j, i) == '#') {
                p.setPen(QPen(Qt::yellow, 3));
                p.setBrush(Qt::yellow);
                p.drawRect(QRect(j * default_multiple, i * default_multiple, default_multiple, default_multiple));
            } else {
                p.setPen(oldPen);
            }
            p.drawText(QPointF(j * default_multiple, (i + 1) * default_multiple), game);

        }
    }

    p.drawText(QPointF((wall->getCol() + 2) * default_multiple, 10 * default_multiple), "a: left");
    p.drawText(QPointF((wall->getCol() + 2) * default_multiple, 12 * default_multiple), "d: right");
    p.drawText(QPointF((wall->getCol() + 2) * default_multiple, 14 * default_multiple), "w: up");
    p.drawText(QPointF((wall->getCol() + 2) * default_multiple, 16 * default_multiple), "s: down");

}

void GameWidget::startGame() {

    if(isStart) {
        return;
    }
    isStart = true;

    update();
}

void GameWidget::stopGame() {

    if(!isStart) {
        return;
    }
    isStart = false;


    update();

}

void GameWidget::destoryGame() {
    if(food != NULL) {
        delete food;
    }

    if(snake != NULL) {
        delete snake;
    }

    if(wall != NULL) {
        delete wall;
    }
}

GameWidget::~GameWidget() {

    if(timer != NULL) {
        timer->stop();
        timer = NULL;
    }

    stopGame();
    destoryGame();

}